首页 > 解决方案 > 在 R 中生成一个单独的页面

问题描述

我希望当我单击搜索按钮时在单独的页面上显示结果,而不是在同一页面上。我已经尝试了两个代码第一个:

用户界面:

ui = fluidPage(
  theme = shinytheme("cerulean"),
  mainPanel(

    div(align = "center", style="margin-left:500px",
        radioButtons("typeInput", "Extract tweets by: ",list("Hashtag" = "hashtag", "Twitter Username"= "username"),inline=T),
          textInput("hashtagInput", "Enter search string","", placeholder = "input search string"),
        conditionalPanel(
          condition = "input.typeInput == 'username'",
          textInput("usernameInput", "Username", placeholder = "input username")),
        sliderInput("numberInput", "Select number of tweets",min = 0, max = 3000, value = 100),
          br(),
        actionButton("goButton", "Search", icon("twitter"),
                     style="color: #fff; background-color: #337ab7"),
        uiOutput("pageStub")
    )))

服务器:

server = function(input, output){
  data = eventReactive(input$goButton, {

    if (input$typeInput == "hashtag") {
      tweetOutput = searchThis(search_string = input$hashtagInput,
                                      number.of.tweets = input$numberInput)} 
    else if (input$typeInput == "username") {
      tweetOutput = userTL(user.name = input$usernameInput,number.of.tweets = input$numberInput)}
    else {}
    library(twitteR)
    df.tweets = data.frame(tweetOutput)
    tweetOutput = df.tweets})
  uiOutput(
    output$pageStub <- renderUI(
      fluidPage(
        fluidRow(
          renderDataTable({data()}, options = list(lengthMenu = c(10, 30, 50), pageLength = 5))))))}

但它在同一页面上显示结果,如此处所示

在此处输入图像描述

我尝试了 shinyBS 库的第二个代码,但我认为窗口太小了

用户界面:

ui = fluidPage(
  theme = shinytheme("cerulean"),
  mainPanel(

    div(align = "center", style="margin-left:500px",
        radioButtons("typeInput", "Extract tweets by: ",list("Hashtag" = "hashtag", "Twitter Username"= "username"),inline=T),
          textInput("hashtagInput", "Enter search string","", placeholder = "input search string"),
        conditionalPanel(
          condition = "input.typeInput == 'username'",
          textInput("usernameInput", "Username", placeholder = "input username")),
        sliderInput("numberInput", "Select number of tweets",min = 0, max = 3000, value = 100),
          br(),
        actionButton("goButton", "Search", icon("twitter"),
                     style="color: #fff; background-color: #337ab7"),

        bsModal("modalExample", "Your result", "goButton", size = "large",dataTableOutput("tweetTable"))
    )))

服务器:

server = function(input, output)
{
  data = eventReactive(input$goButton, {

    if (input$typeInput == "hashtag") 
    {
      tweetOutput = searchThis(search_string = input$hashtagInput,
                                      number.of.tweets = input$numberInput)
    } 
    else if (input$typeInput == "username") 
    {
      tweetOutput = userTL(user.name = input$usernameInput,number.of.tweets = input$numberInput)
    }
    else {}
    library(twitteR)
    df.tweets = data.frame(tweetOutput)
    tweetOutput = df.tweets

  })

 output$tweetTable =renderDataTable({data()}, options = list(lengthMenu = c(10, 30, 50), pageLength = 5))
}

如此处所示:

在此处输入图像描述

这是我调用的搜索功能:

searchThis = function(search_string,number.of.tweets = 100)
{
  search_tweets(search_string,n = number.of.tweets, lang = "en")
}


userTL = function(user.name,number.of.tweets = 100)
{
  userTimeline(user.name,n = number.of.tweets)
}

有没有其他方法可以做到这一点?谢谢你

标签: htmlrshiny

解决方案


如果您想使用模态框,您可以修改宽度,使其全屏显示,在 UI 中使用以下行:

tags$head(tags$style(".modal-dialog{ width:100%; overflow-x: scroll;}"))
# width :100% enables you to choose the width of the modal, it could be 95%,50% ...
# overflow-x:scroll displays a horizontal scrollbar if the content is too large for the modal

你的用户界面会是

ui = fluidPage(
  theme = shinytheme("cerulean"),
  mainPanel(

  tags$head(tags$style(".modal-dialog{ width:100%; overflow-x: scroll;}")),

    div(align = "center", style="margin-left:500px",
        radioButtons("typeInput", "Extract tweets by: ",list("Hashtag" = "hashtag", "Twitter Username"= "username"),inline=T),
          textInput("hashtagInput", "Enter search string","", placeholder = "input search string"),
        conditionalPanel(
          condition = "input.typeInput == 'username'",
          textInput("usernameInput", "Username", placeholder = "input username")),
        sliderInput("numberInput", "Select number of tweets",min = 0, max = 3000, value = 100),
          br(),
        actionButton("goButton", "Search", icon("twitter"),
                     style="color: #fff; background-color: #337ab7"),

        bsModal("modalExample", "Your result", "goButton", size = "large",dataTableOutput("tweetTable"))
    )))

推荐阅读