小编tho*_*hal的帖子

将单元格的完整格式存储在变量中

背景

我想编写一个简单的函数来交换两个选定的(不一定是相邻的)单元格的内容。我不想先将单元格复制到临时单元格。因此,我真的很想将单元交换到位。

挑战

虽然通过使用保存Variant单元格 1 内容的临时变量,用单元格 2 的内容覆盖单元格 1 的内容,然后将变量变量的内容写回单元格 2,简单地交换内容相当容易,但我很难如何还复制所有格式相关的内容。有很多插槽需要考虑(.NumberFormat, .Interior仅举两个)。我真的需要单独复制它们吗?还是有更简单的方法来交换格式而不使用任何临时单元格?

代码

Public Sub SwapCells(Optional bolWithFormat As Boolean = True)
    'Purpose: switch the content of two cells
    On Error GoTo ErrHandler
    Dim rngSel As Range
    Dim varContent As Variant
    Set rngSel = Selection
    If (rngSel.Count = 2) Then
        With rngSel.Cells(1)
            varContent = .Value
            .Value = rngSel.Cells(2).Value
            rngSel.Cells(2).Value = varContent
        End With
    Else
        'Do nothing, because swap makes only sense for …
Run Code Online (Sandbox Code Playgroud)

excel vba

5
推荐指数
1
解决办法
4928
查看次数

反应性地更新 Shiny 中的滑块输入

我正在尝试从sliderInput动态更改值。现在的困难是我想从sliderInput一个值变成sliderInput一个范围,这似乎不起作用。

下面代码中的第一个 actionbutton 有效,而第二个却没有它的意图。

是切换到uiOutput元素的唯一可能性吗?

代码

library(shiny)
app <- shinyApp(
   ui = bootstrapPage(
      sliderInput("sld1", min = 0, max = 10, label = "y1", value = 5),
      actionButton("acb1", "Change Value"),
      actionButton("acb2", "Change Value to Range")
   ),
   server = function(input, output, session) {
      observeEvent(input$acb1, {
         updateSliderInput(session, "sld1", value = 2)
      })
      observeEvent(input$acb2, {
         updateSliderInput(session, "sld1", value = c(2,7))
      })
   })
runApp(app)
Run Code Online (Sandbox Code Playgroud)

r shiny

5
推荐指数
1
解决办法
3096
查看次数

在selectInput旁边放置按钮

目标

我要放置selectInputactionButton并排在我的页脚侧shinydashboard::box.selectInput 无论盒子的宽度如何,按钮都应该"相对靠近" .

到目前为止我尝试过的

到目前为止,我尝试过column,splitLayout或通过样式display: inline-block,但我不满意任何一种解决方案:

  • column:取决于盒子的宽度,selectInput和之间的间隙actionButton太大(我可以通过将selectInput宽度扩展到100%来部分解决,但宽度太大)
  • splitDesign:到目前为止最好的选择,但cellWidths需要基于盒子的调整width,也只有100%的selectInput宽度工作,对于大盒子,第二次分割的宽度似乎太大了
  • inline-block:与一般不搭配 CSS

library(shiny)
library(purrr)
library(shinydashboard)

widths <- c(1, 2,3, 4, 6, 12)

makeBoxes <- function(width, method = c("split", "col", "css")) {
   method <- match.arg(method)
   split <- function(width, count) {
      splitLayout(selectInput(paste("sel", width, count, sep = "_"), NULL, LETTERS,
                              width = …
Run Code Online (Sandbox Code Playgroud)

css twitter-bootstrap shiny shinydashboard

5
推荐指数
1
解决办法
314
查看次数

如何安装GDAL以在R中使用rgdal?

我从 GISInternals 下载了带有 python 绑定的 GDAL,我的计算机上已经安装了 python。但是当我使用 R 时,它不会检测 gdal 安装,而是在使用 command 时给出错误gdal_setinstallation()。如何消除此错误以便 R 可以找到已安装的 GDAL。

r gdal

5
推荐指数
1
解决办法
1万
查看次数

单词后获取文本--R Web抓取

几周前,这里有人帮助我极大地获得了Notable Names数据库中所有链接的列表。我能够运行此代码并获得以下输出

library(purrr)
library(rvest)
url_base <- "https://www.nndb.com/lists/494/000063305/"    
## Gets A-Z links
all_surname_urls <- read_html(url_base) %>%
      html_nodes(".newslink") %>%
      html_attrs() %>%
      map(pluck(1, 1))

all_ppl_urls <- map(
      all_surname_urls, 
      function(x) read_html(x) %>%
        html_nodes("a") %>%
        html_attrs() %>%
        map(pluck(1, 1))
    ) %>% 
      unlist()

all_ppl_urls <- setdiff(
      all_ppl_urls[!duplicated(all_ppl_urls)], 
      c(all_surname_urls, "http://www.nndb.com/")
    )

all_ppl_urls[1] %>%
      read_html() %>%
      html_nodes("p") %>%
      html_text()

# [1] "AKA Lee William Aaker"
# [2] "Born: 25-Sep-1943Birthplace: Los Angeles, CA"
# [3] "Gender: MaleRace or Ethnicity: WhiteOccupation: Actor"
# [4] "Nationality: United StatesExecutive summary: The Adventures …
Run Code Online (Sandbox Code Playgroud)

r web-scraping

5
推荐指数
1
解决办法
175
查看次数

如何从一个圆的多个角度计算最常见的鸟类飞行方向?

我正在计算鸟类在给定区域(机场)上最常见的飞行路线。我知道他们的位置(与我的距离)和他们的飞行角度。我位于某个特定地点,鸟儿在我周围飞来飞去。我假设所有的鸟都是直线飞行。

我怎么知道该地区最常见的飞行路线是什么?

航角示例:

direction <- c(35, 70, 300, 260, 340, 130, 240, 40, 190, 190, 150, 20)
Run Code Online (Sandbox Code Playgroud)

考虑到我的距离和角度,我绘制了他们的位置。然后,我添加了他们的飞行角度和飞行距离,以查看飞行路线(1.5公里)。

在此处输入图片说明

如您所见,这有点混乱,但我想大致了解一下鸟类是否在某个角度范围(20-30°范围)内飞行得更频繁,或者是否都是随机的。

math geometry r

5
推荐指数
1
解决办法
64
查看次数

scale _ * _ manual中调色板参数的用法

背景

的帮助?scale_fill_manual说:

 ...: Arguments passed on to ‘discrete_scale’
          palette A palette function that when called with a single
              integer argument (the number of levels in the scale)
              returns the values that they should take.
Run Code Online (Sandbox Code Playgroud)

因此,我尝试:

library(ggplot)
library(dplyr)

my_pal <- colorRampPalette(1:3)

(p <- ggplot(mtcars %>% 
              group_by(cyl) %>% 
              summarise(mpg = mean(mpg)), 
             aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
   geom_col())
Run Code Online (Sandbox Code Playgroud)

问题

但是,如果我想将调色板功能添加到比例尺,它将无法正常工作:

p + scale_fill_manual(palette = my_pal)
# Error in as.vector(x, "character") : 
#   cannot coerce type …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

5
推荐指数
1
解决办法
54
查看次数

为类构造函数提供默认值的最pythonic方式

我试图坚持谷歌的风格指南,从一开始就努力保持一致性。

我目前正在创建一个模块,在这个模块中我有一个类。我想为不同的标准用例提供一些合理的默认值。但是,我想让用户灵活地覆盖任何默认值。我目前正在做的是提供一个具有默认值(针对不同用例)的模块范围的“常量”字典,并且在我的类中,我在构造函数中提供参数优先于默认值。

最后,我想确保我们以参数的有效值结束。

这就是我所做的:

MY_DEFAULTS = {"use_case_1": {"x": 1, "y": 2},
               "use_case_2": {"x": 4, "y": 3}}

class MyClass:
   def __init__(self, use_case = None, x = None, y = None):
      self.x = x
      self.y = y
      if use_case:
         if not self.x:
            self.x = MY_DEFAULTS[use_case]["x"]
         if not self.y:
            self.y = MY_DEFAULTS[use_case]["y"]
      assert self.x, "no valid values for 'x' provided"
      assert self.y, "no valid values for 'y' provided"
   def __str__(self):
      return "(%s, %s)" % (self.x, self.y)  

print(MyClass()) # AssertionError: no valid …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
103
查看次数

通过正则表达式根据字符串中的位置替换子字符串

假设我的字符串中有一个特定的模式,该模式出现了已知的次数 ( n),并且我们不想对字符串的其余部分(特别是这些模式之间的字符串)做出任何假设。

此外,我有一个长度向量nsf例如),我想用相应的元素修改模式的每次出现。因此,对于每场比赛,我想知道比赛已经发生了多少次?

我可以想到以下解决方案:

library(stringr)
sf <- letters[4:1]
ss <- "fdskjhf xx sd ss xx wwwe xx ss  xx sdsd"
#              ^^ 1st   ^^ 2nd  ^^ 3rd ^^ 4th
# add:         _sf[1]   _sf[2]  _sf[3] _sf[4]
# that is:     xx_d     xx_c    xx_b   xx_a


## add _sf[i] to the ith occurence of "xx" in ss
goal <- "fdskjhf xx_d sd ss xx_c wwwe xx_b ss  xx_a sdsd"

my_replacer_factory <- function(sf, start = 0) {
  cnt <- …
Run Code Online (Sandbox Code Playgroud)

regex r stringr

5
推荐指数
1
解决办法
87
查看次数

Shinyjs:[添加|删除]类在模块中不起作用

背景

在我的shiny模块中,我进行了长时间的计算。我想通过将光标更改为旋转圆圈来向用户提供一些视觉反馈。为此,我创建了一个css.wait,并希望使用shinyjs::addClassbody显示新光标。然而,在模块内这不起作用,但它在主应用程序中起作用。如何将所需的行为添加到我的模块中?

代码

library(shiny)
library(shinyjs)

testUI <- function(id) {
   ns <- NS(id)
   tagList(useShinyjs(),
           inlineCSS('.wait {cursor: wait;};'),
           actionButton(ns("wait"), "wait"),
           actionButton(ns("stop"), "stop"))
}

test <- function(input, output, session) {
   observeEvent(input$wait,  addClass(selector = "body", class = "wait"))
   observeEvent(input$stop,  removeClass(selector = "body", class = "wait"))
}

ui <- fluidPage(
   useShinyjs(),
   testUI("test"),
   div("Test to show that 'wait' class works", class = "wait"),
   actionButton("wait.main", "wait"),
   actionButton("stop.main", "stop")
) 
server <- function(input, output, session) {
    callModule(test, "test") …
Run Code Online (Sandbox Code Playgroud)

r shiny shinyjs

4
推荐指数
1
解决办法
1598
查看次数