【问题标题】:Shiny values within a function from input来自输入的函数中的闪亮值
【发布时间】:2017-09-22 19:32:25
【问题描述】:

我正在开发一个闪亮的应用程序,我需要输入值才能显示在函数中。

我要做的是以某种方式从输入中提取名称和值,例如CL=4V1=3,然后将它们提供给我需要稍后提供给服务器中的模型的函数。我正在研究的模型只采用函数格式。下面是我的代码。

用户界面:

#Set up
rm(list=ls())

library(shiny)
library(shinydashboard)
library(shinyBS)
library(dplyr)

#Design sidebar
sidebar <- dashboardSidebar(width = 200, collapsed=TRUE,
                            sidebarMenu(id="tabs",
                                        menuItem("Simulations", tabName = "Sims", icon = icon("line-chart"), selected=TRUE),
                                        menuItem("Placeholder", tabName = "Place", icon = icon("square-o"))
                            ))

#Design body 
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "Sims", 
            box(collapsible=TRUE, width = 3, status = "success", solidHeader = T, title="Select Your Model",
                radioButtons(inputId="PK_Model", label = "PK Model", selected="One_Comp", 
                             choices = c("1 Compartment"="One_Comp", "2 Compartment"="Two_Comp")),
                radioButtons(inputId="Clearance_Model",label ="Clearance Mechanism", selected="Linear", 
                             choices = c("Linear"="Linear"))),
            box(collapsible=TRUE, status = "success", solidHeader = T, title="Enter Parameter Estimates", 
                        conditionalPanel(condition = "input.Clearance_Model == 'Linear'",
                                          numericInput(label="Clearance (Linear) (CL; L/day)", inputId="CL", min = 0, max = NA, value = 0),
                                          numericInput(label="Central volume of distribution (V1; L)", inputId="V1", min = 0, max = NA, value = 0)),
                         conditionalPanel(condition = "input.PK_Model == 'Two_Comp'",
                                          numericInput(label="Inter-compartment clearance (Q; L/day)", inputId="Q", min = 0, max = NA, value = 0),
                                          numericInput(label="Peripheral volume of distribution (V2; L)", inputId="V2", min = 0, max = NA, value = 0))),
              box(collapsible=T, width=2, status = "success", solidHeader = T, title="Run",
                actionButton('gosim','Run Sims',class='btn btn-info', icon=icon('play-circle-o','fg-lg'))), 
              box(width=5, status = "success", solidHeader = T, title="Simulated", textOutput('testprint')))))

#Show title and the page (includes sidebar and body)
dashboardPage(skin=c("blue"),
              dashboardHeader(titleWidth=900, title = "Shiny Test"),
              sidebar,  body)

更新: 按照下面 PoGibas 的回答,我将服务器代码更新为以下,但结果看起来不像我需要的。

Server:

    library(shiny)
    library(shinydashboard)
    library(shinyBS)
    library(RxODE)
    library(parallel)
    library(ggplot2)

test <- function (N1, N2, N3, N4) {
  mypar <- function(lKa, lKdeg, N1, N2, N3, N4){
    Ka=exp(lKa)
    Kdeg=exp(lKdeg)
    V1=N1
    CL=N2
    Q=N3
    V2=N4}
  return(mypar)
}

    shinyServer(function(input, output, session){

 mypar <-eventReactive(input$goSimPH20, {
  N1=as.numeric(input$V1)
  N2=as.numeric(input$CL)
  N3=as.numeric(input$Q)
  N4=as.numeric(input$V2)
  par1 = test(N1, N2, N3, N4)
  return(par1)
})

        output$testprint <- renderText(mypar())  

        })

当我在服务器中调用 mypar() 时,我需要的结果如下:

test <- function (lKa, lKdeg, V1, CL, Q, V2) {
            Ka=exp(lKa)
            Kdeg=exp(lKdeg)
            V1=xx
            V2=xx
            CL=xx
            Q=xx
            }

xx 可以是用户界面中任何用户提供的值。

【问题讨论】:

  • 如果我的解决方案有助于解决您的问题,您可以接受答案,以便我们关闭问题:)
  • 请将您的代码重写为最小示例。 stackoverflow.com/help/mcve
  • @Krina 如果它有助于解决您的问题,请您接受答案吗?

标签: r shiny


【解决方案1】:

我将服务器部分简化为:

test <- function (V1 = 1, CL = 2, lA = 1, lB = 2) {
    return(lA + lB + V1 + CL)
}

shinyServer(function(input, output, session) {
    mypar <- reactive({
        V1 = as.numeric(input$V1)
        CL = as.numeric(input$CL)
        return(test(V1, CL))
    })
    output$testprint <- renderText(mypar())  
})

test 函数中添加您想要的公式。使用V1 = as.numeric(input$V1) 提取用户输入并使用test(V1, CL) 传递给test

【讨论】:

  • 感谢您的回复。我仍在为此苦苦挣扎。调用 mypar 作为回报,我需要一个如下所示的函数: test
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 2021-12-17
  • 2018-09-05
相关资源
最近更新 更多