加载Javascript后,反应式输入不起作用

时间:2019-03-10 18:06:41

标签: r shiny shinydashboard shinyjs shiny-reactivity

我想在我闪亮的应用程序中添加蛋白质查看器。加载脚本后,我的应用不再响应我的任何输入。以下是使用mtcars数据集的我的应用程序的简化版本。

我的代码:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Example"),
  dashboardSidebar(
    sidebarMenu(id = "sidebarmenu",
           selectizeInput("sample2", "Sample: ",
                               choices = c("cyl", "mpg", "disp", "hp")))),
  dashboardBody(
     fluidRow(
      box(title="3D Structure", width = 6, status="primary", solidHeader =TRUE, 
          uiOutput("structure_3d"))),
  fluidPage( 
    box(status="primary", solidHeader = FALSE, align = "center",
        DT::dataTableOutput("table")))))

server <- function(input, output, session) {

mtcars2 <- reactive({

  mtcars2 <- mtcars

  return(mtcars2)
}) 

output$table <- DT::renderDataTable({

mtcars2 <- mtcars2()
mtcars2 <- subset(mtcars2, select = c(input$sample2))

  table <- DT::datatable(mtcars2, rownames = FALSE)

})

output$structure_3d <- renderUI({

  tabPanel("3D Structure", 
           tags$head(tags$script(src="http://3Dmol.csb.pitt.edu/build/3Dmol-min.js")),
           tags$div(
             style="height: 400px; width: 700px: position: relative;",
             class='viewer_3Dmoljs',
             'data-pdb'='2POR',
             'data-backgroundcolor'='0xffffff',
             'data-style'='cartoon'))

})

}

shinyApp(ui = ui, server = server)

如何让应用再次对输入做出反应?

编辑:

我也尝试将脚本移至UI,但表仍未响应输入:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Example"),
  dashboardSidebar(
    sidebarMenu(id = "sidebarmenu",
           selectizeInput("sample2", "Sample: ",
                               choices = c("cyl", "mpg", "disp", "hp")))),
  dashboardBody(
     fluidRow(
      box(title="3D Structure", width = 6, status="primary", solidHeader =TRUE, 
          tags$head(tags$script(src="http://3Dmol.csb.pitt.edu/build/3Dmol-min.js")),
          tags$div(
            style="height: 400px; width: 700px: position: relative;",
            class='viewer_3Dmoljs',
            'data-pdb'='2POR',
            'data-backgroundcolor'='0xffffff',
            'data-style'='cartoon'))),
      fluidRow( 
        box(status="primary", solidHeader = FALSE, align = "center",
            DT::dataTableOutput("table")))))

server <- function(input, output, session) {

mtcars2 <- reactive({

  mtcars2 <- mtcars

  return(mtcars2)
}) 

output$table <- DT::renderDataTable({

mtcars2 <- mtcars2()
mtcars2 <- subset(mtcars2, select = c(input$sample2))

  table <- DT::datatable(mtcars2, rownames = FALSE)

})

}

shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:0)

尝试将这些代码和javascript标签专门移到您的UI中。当前的renderUI()不使用任何反应式内容。还要检查您的JavaScript控制台,以查看脚本是否引发任何错误。在下面,将其添加为创建UI时加载的库是一种更好的形式。如果您需要在反应式用户界面中调用javascript函数,则可以专门调用3Dmol-min.js脚本中正在加载的函数。

 dashboardPage(
    tags$head(tags$script(src="http://3Dmol.csb.pitt.edu/build/3Dmol-min.js")),

答案 1 :(得分:0)

3Dmol jQuery和闪亮的jQuery之间存在冲突。

如果运行nojquery脚本,它将起作用:

tags $ head(tags $ script(src =“ http://3Dmol.csb.pitt.edu/build/3Dmol-nojquery.js”))