Henrik Bengtsson为互联网提供了一种在R中创建S3泛型的好方法,而不必担心它们是否在2002年之前创建过.
他的函数setGenericsS3的作用基本上是:
如果是这样,
如果没有,只需创建通用.
事实证明,当您自己的软件包中没有可用的代码时,自动创建泛型非常有用.当我们走过这个R时代时,我想知道现在在R中实现相同的正确方法是什么.我isS3Generic()
在R中找不到或类似的函数,Henrik Bengtsson的代码源自R 2.14中引入的强制命名空间.我记得我已经看到了实现同样目标的其他方法,但找不到它们.
编辑:我特意找S3.该功能isGeneric()
仅适用于S4,例如anova
(用于S3通用):
> isGeneric('anova')
[1] FALSE
> anova
function (object, ...)
UseMethod("anova")
<bytecode: 0x04dc7a18>
<environment: namespace:stats>
Run Code Online (Sandbox Code Playgroud) 从 Plotly 4.5.6 升级到 Plotly 4.6.0(或更高版本)后,在plot_ly
. 这发生在窗口大小调整或 tabsetPanel 选择时。
先决条件
plot_ly(..., height=<some dependency>)
<some dependency>
是一个反应值(input$height
在下面的例子中)问题
例子
selectInput
(100px)的第一个值,即使此值根本没有更改:如果tabsetPanel
页面中有 ,也会发生同样的问题(例如在绘图下方)。单击不同的选项卡时,绘图高度会更改。
library(shiny)
library(plotly)
ui <-shinyUI(fluidPage(
selectInput("height", "Choose desired height", choices=c(100,800)),
plotlyOutput("plot")))
server <- shinyServer(function(input,output, session){
output$plot <- renderPlotly({
plot_ly(mtcars, x=~gear, y=~cyl, height = input$height)
})
})
shinyApp(ui,server)
Run Code Online (Sandbox Code Playgroud)
到目前为止我尝试过的
height
参数放入renderPlotly
工作中,但由于 Plotly 本身从不改变其高度,因此绘图本身始终保持在 800px(只是容器高度发生了变化):用户界面: uiOutput("plot")
服务器:
output$plot <- renderUI({ plotlyOutput("plotly", …
Run Code Online (Sandbox Code Playgroud) 我想使用闪亮的js函数将数据从javascript发送到R,但无法正常工作。我所做的是一个简单的示例,其中setinputValue将“ noone”发送给“ too” input $ too
这是代码:
library(shiny)
ui <- fluidPage(
HTML("<script>
$( document ).ready(function() {
Shiny.setInputValue('too', 'noone');
});</script>"),
textOutput("table")
)
server <- function(input, output) {
output$table <- renderPrint(input$too)
}
shinyApp(ui,server)
Run Code Online (Sandbox Code Playgroud)
我得到的js错误是:Shiny.setInputValue不是一个函数
event_data("plotly_click")
用户单击散点图中的标记后,我正在使用Plotly的东西(打开模式)。之后(例如,关闭模态),event_data("plotly_click")
当然不会改变,因此单击相同的标记不会再次触发相同的动作。
最小示例:
library(plotly)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
mtcars %>% plot_ly(x=~disp, y=~cyl)
})
# Do stuff after clicking on a marker in the plot
observeEvent(event_data("plotly_click"), {
print("do some stuff now") # this is not executed after second click on same marker
})
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了使用Shinyjs的变通办法onclick
,但无济于事(它在绘图的空白区域工作良好,但在单击标记时无效):
shinyjs::onclick(id="plot", print("clicked"))
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用反应性值来存储最后的点击,然后立即将其重置(例如,通过event_data("plotly_hover")
),但是所有尝试均会失败,因为它event_data("plotly_click")
仍然保持其旧值。
有人可以帮忙吗?
有人可以帮我在R Shiny中绘制一个仪表图吗?我不需要它是动态的,但要有一个我赋值的KPI,并显示当我运行app时它应该是红色从0到0.3,黄色0.3至0.5,绿色0.5至1.